package com.xuecheng.content.controller;

import com.xuecheng.api.content.TeachplanApi;
import com.xuecheng.api.content.model.dto.TeachplanDTO;
import com.xuecheng.api.content.model.dto.TeachplanMediaDTO;
import com.xuecheng.api.content.model.vo.BindTeachplanMediaVO;
import com.xuecheng.api.content.model.vo.TeachplanVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.TeachplanConverrt;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程计划 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class TeachplanController implements TeachplanApi {

    @Autowired
    private TeachplanService  teachplanService;

    @GetMapping("teachplan/{courseBaseId}/tree-nodes")
    public TeachplanDTO getTreeNodes(@PathVariable Long courseBaseId) {

        Long companyId = SecurityUtil.getCompanyId();

        TeachplanDTO dto = teachplanService.getTreeNodes(courseBaseId, companyId);

        return dto;
    }


    @PostMapping("teachplan")
    public TeachplanDTO createOrModifyTeachplan(@RequestBody TeachplanVO vo) {
        Long companyId = SecurityUtil.getCompanyId();

        TeachplanDTO dto = TeachplanConverrt.INSTANCE.vo2dto(vo);

        return teachplanService.createOrModifyTeachplan(dto,companyId);


    }

    @PostMapping("teachplan/media/{teachplanMediaId}")
    public TeachplanMediaDTO associationMedia(@RequestBody BindTeachplanMediaVO vo) {
        Long companyId = SecurityUtil.getCompanyId();

        TeachplanMediaDTO teachplanMediaDTO = new TeachplanMediaDTO();
        teachplanMediaDTO.setTeachplanId(vo.getTeachplanId());
        teachplanMediaDTO.setMediaId(vo.getMediaId());

        //课程计划一致为主业服务，需要在teachplanservice
        TeachplanMediaDTO result = teachplanService.associationMedia(teachplanMediaDTO, companyId);

        return result;


    }
}
